let mta = require('../../utils/mta_analysis.js');
Page({
	data: {

		inputCode: '',
		isScan: false,
		isShow: false,
		obj: null,
		urlAddress: getApp().globalData.imageUrl,
		isEmpty: true,
		focus: false

	},
	onLoad: function(options) {
		mta.Page.init();
	},
	onReady: function() {},
	onShow: function() {
		if (getApp().globalData.common.isScanPage) {
			if (getApp().globalData.common.deviceCode) {

				this.getcashierSettlement();
			} else if (getApp().globalData.common.gbk) {

				this.addShopMarket(getApp().globalData.common.gbk, 1);
			} else {
				this.getShopMarket();
			}
		}
		getApp().globalData.common.isAuto = '';
		getApp().globalData.common.voucherMemberId = '';
	},

	onHide: function() {},
	onUnload: function() {},
	getcashierSettlement: function() {
		var _self = this;
		wx.showLoading({
			title: '加载中',
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/cart/cashierSettlement.do',
			data: {
				shopCode: getApp().globalData.common.shopCode,
				deviceCode: getApp().globalData.common.deviceCode
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading()
				_self.setData({
					obj: res.data.obj
				});
				console.log(_self.data.obj)
				if (_self.data.obj.cartItems.length == 0) {

					console.log("_self.obj.cartItems.length")
					return
				}
				getApp().globalData.common.deviceCode = null;
				_self.submitAction();
			}
		})
	},
	getShopMarket: function() {
		var _self = this;
		wx.showLoading({
			title: '加载中',
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/cart/getCartGoods.do',
			data: {
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading()
				_self.setData({
					obj: res.data.obj
				});
			}
		})
	},
	showInput: function() {
		mta.Event.stat("smg_btshoudongshuru", {});
		this.setData({
			isShow: !this.data.isShow,
			focus: true
		});
	},
	cancelAction: function() {
		this.setData({
			focus: false,
			isShow: !this.data.isShow

		});

	},
	CodeInput: function(e) {
		this.setData({
			inputCode: e.detail.value
		})
	},
	makeSure: function() {
		if (this.data.inputCode == '') {

		} else {
			var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
			if (!pattern.test(this.data.inputCode)) {
				// this.addShopMarket(this.data.inputCode, 1);

				this.filterNum(this.data.inputCode);
			} else {
				_self.showMyToast(_self, "非商品条码")

			}
		}

	},
	addShopMarket: function(code, num) {

		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/cart/addGoods.do',
			data: {
				gbk: code,
				shopCode: getApp().globalData.common.shopCode,
				num: num
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					_self.setData({
						obj: res.data.obj,
						isShow: false
					});
					// console.log(res.data.obj.lastGoodsisNull)
					if (res.data.obj.lastGoodsisNull & num == 1) {
						_self.showMyToast(_self, "没有该商品")
					}
					//console.log(getApp().globalData.common.gbk);
					if (getApp().globalData.common.gbk) {
						console.log("*****")
						console.log(getApp().globalData.common.gbk)
						getApp().globalData.common.gbk = "";
						getApp().globalData.common.TS = "";
						wx.navigateTo({
							url: '../pay/pay'
						})
					}
				} else if (res.data.code == 507) {

					_self.setData({
						isShow: false
					});
					// console.log(res.data.obj.lastGoodsisNull)

					_self.showMyToast(_self, res.data.des)



				}



			}
		})
	},
	addAction: function(e) {
		mta.Event.stat("smg_btgouwuchejia", {});
		this.addShopMarket(e.currentTarget.dataset.code, -1);
	},
	subAction: function(e) {
		mta.Event.stat("smg_btgouwuchejian", {});
		this.addShopMarket(e.currentTarget.dataset.code, 1);
	},
	submitAction: function() {
		mta.Event.stat("smg_btgouwuchejiesuan", {});
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		getApp().globalData.common.isScanPage = true;
		wx.navigateTo({
			url: '../pay/pay'
		})
	},
	cleanAll: function() {
		mta.Event.stat("smg_btgouwucheqingkong", {});
		if (this.data.obj) {
			var _self = this;
			wx.showLoading({
				title: '加载中',
			})
			wx.request({
				url: getApp().globalData.baseUrl + '/applet/cart/clear.do',
				data: {
					shopCode: getApp().globalData.common.shopCode,
				},
				header: getApp().globalData.header,
				success: function(res) {
					wx.hideLoading()
					_self.setData({
						obj: res.data.obj,
					});
				}
			})
		}
	},
	scanAction: function() {
		getApp().globalData.common.isScanPage = false; //扫码不刷新购物车
		mta.Event.stat("smg_btgouwuchesaoma", {});
		var _self = this;
		wx.scanCode({
			success: (res) => {
				var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
				if (!pattern.test(res.result)) {
					// _self.addShopMarket(res.result, 1);
					_self.filterNum(res.result);
				} else {
					_self.showMyToast(_self, "非商品条码")
				}
			},
			fail:(err)=>{
			console.log(err);
			}
		})
	},
	showMyToast: function(self, my_toast_tip, time = 1000, template = 'my_toast') {
		self.setData({
			template: template, //my_toast
			my_toast_tip: my_toast_tip
		})
		var timer = setTimeout(() => {
			self.setData({
				template: '', //my_toast
				my_toast_tip: ''
			})
			clearTimeout(timer);
		}, time)

	},
	/// 长按
	longTap: function(e) {
		var _self = this;
		wx.showModal({
			title: '提示',
			content: '是否删除' + e.currentTarget.dataset.name + '商品',
			showCancel: true,
			success: function(res) {
				if (res.confirm) {
					wx.request({
						url: getApp().globalData.baseUrl + '/applet/cart/delete.do',
						data: {
							shopCode: getApp().globalData.common.shopCode,
							goodsCode: e.currentTarget.dataset.code
						},
						header: getApp().globalData.header,
						success: function(res) {
							wx.hideLoading()
							_self.setData({
								obj: res.data.obj,
							});
						}
					})
				}
			}
		})
	},
	addPayGoods: function(code) {
		var _self = this;

		wx.request({
			url: getApp().globalData.baseUrl + '/applet/pay/getPayAmount.do',
			data: {
				gbk: code,
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res);
				if (res.data.code == 200) {
					_self.payGoodsAction(res.data.obj.amount, code)
				} else {
					_self.showMyToast(_self, res.data.des)

				}
			}
		})

	},
	payGoods: function(code) {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/pay/payBalance.do',
			data: {
				gbk: code,
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					wx.showModal({
						title: '提示',
						content: '支付成功' + res.data.obj.amount + '元',
						showCancel: false,
						confirmText: "完成",
						confirmColor: "#ff2f31",
						success: function(res) {
							if (res.confirm) {

							}
						}
					})

				} else {
					_self.showMyToast(_self, res.data.des)
				}



			},
			fail: function() {
				console.log(1)
			}
		})

	},
	filterNum: function(code) {
		if (code.indexOf("TP") > -1) {　
			this.setData({
				isShow: false
			});　
			//console.log(code);
			this.addPayGoods(code);
		} else {
			this.addShopMarket(code, 1);
		}
	},
	payGoodsAction: function(num, code) {
		// mta.Event.stat("smg_btshouyesaoma", {});
		var _self = this;
		wx.showModal({
			title: '提示',
			content: '确认支付' + num + '元',
			showCancel: true,
			success: function(res) {
				if (res.confirm) {
					console.log('付钱')
					_self.payGoods(code);
				}
			}
		})
	}

})